const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({
	data: {
		leavereason:"",	//请假事由
    leaveDay: "",
    studentList:[],
    student_id:0,
    student_index:0
	},
	onReady: function() {
		if (!app.login()) {
			return;
		}
	},
	onLoad(options) {
    this.getStudentList(); //获取选择请假学生列表
  },
  getStudentList() {//选择学生
    var that = this;
    var _student_id = 0;
		http.httpPost(config.getLeaveStudentList, {}, function(e) {
      var _data = e.data;
      if(_data.length==0){
          wx.showToast({
            icon:'none',
            title: e.msg
          })
      }else{
        _student_id = _data[0].id;
      }
			that.setData({
        'studentList': e.data,
        'student_id':_student_id
			});
		});
	},
	//请假时间
	leaveDayChange: function(e) {
		this.setData({
			leaveDay: e.detail.value,
    });
  },
  //选择学生
	studentChange: function(e) {
    let student_id = this.data.student_id;
		student_id = this.data.studentList[e.detail.value].id;
		this.setData({
      student_id: student_id,
      student_index:e.detail.value
		});
	},
	getLeavereason:function(e){	//获取请假事由值
		let _leavereason = e.detail.value;
		this.setData({
			leavereason: _leavereason
		});
	},
	addStudentLeave() {//添加学生订餐请假
		var _leavereason = this.data.leavereason;
    var _leavetime = this.data.leaveDay;
    var _student_id = this.data.student_id;
    
    if(_student_id==0){
			wx.showToast({
				title: '请选择【请假学生】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
    }
    
		if(_leavereason=="" || _leavereason==undefined){
			wx.showToast({
				title: '请输入【请假事由】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}

		if(_leavetime=="" || _leavetime==undefined){
			wx.showToast({
				title: '请选择【请假时间】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}

		http.httpPost(config.addLeaveForDinner, {
			leavereason:_leavereason,
      leavetime:_leavetime,
      student_id:_student_id
		}, function(e) {
      wx.showToast({ title: e.msg, icon: 'none', duration: 1000, mask: true });
      setTimeout(function(){
         wx.reLaunch({
           url: '/pages/leave_for_dinner/leave_list',
         })}
      , 1000);
			
		});
		
	},

})
